package com.brayan.webapp.controller.general.i18n;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import com.brayan.webapp.model.general.i18n.Locale;
import com.brayan.webapp.model.general.i18n.Text;
import com.brayan.webapp.model.general.i18n.Translation;
import com.brayan.webapp.repository.general.i18n.LocaleRepository;
import com.brayan.webapp.repository.general.i18n.TextRepository;
import com.brayan.webapp.repository.general.i18n.TranslationRepository;

@Named
@ConversationScoped
public class TranslationController implements Serializable {

	private static Logger LOGGER = Logger.getLogger(TextRepository.class);

	private static final long serialVersionUID = -8330102208136353780L;

	@Inject
	private TextRepository textRepo;
	@Inject
	private TranslationRepository transRepo;
	@Inject
	private LocaleRepository localeRepo;

	private Long id;
	private Long textUnitId;
	private Long localeId;
	private String languageCode;
	private String countryCode;
	private String translation;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getTextUnitId() {
		return textUnitId;
	}

	public void setTextUnitId(Long textUnitId) {
		this.textUnitId = textUnitId;
	}

	public Long getLocaleId() {
		return localeId;
	}

	public void setLocaleId(Long localeId) {
		this.localeId = localeId;
	}

	public String getLanguageCode() {
		return languageCode;
	}

	public void setLanguageCode(String languageCode) {
		this.languageCode = languageCode;
	}

	public String getCountryCode() {
		return countryCode;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public String getTranslation() {
		return translation;
	}

	public void setTranslation(String translation) {
		this.translation = translation;
	}

	/**
	 * Pass the locale code (e.g. de-CH) to load and retrieve a translation.
	 * 
	 * @param localeCode
	 * @return
	 */
	public Translation getTranslationByTextIdAndLocaleId(Long textId, Long localeId) {
		Translation trans = transRepo.getTranslationByTextIdAndLocaleId(textId, localeId);
		return trans;
	}

	/**
	 * Provide a full locale code. e.g. de-CH or a semi-complete locale code
	 * "de" to retrieve a translation. NOTICE, if the full locale does not
	 * return a value, the query with the semi-complete locale is conducted.
	 * 
	 * @param textId
	 * @param localeCode
	 * @return
	 */
	public Translation getTranslationByTextIdAndLocaleCode(Long textId, String localeCode) {
		String langCode = localeCode.substring(0, 2); // e.g. de
		String countryCode = null;
		if (localeCode.length() == 5)
			countryCode = localeCode.substring(3, 5); // e.g CH
		else
			countryCode = "";
		Locale locale = localeRepo.getLocaleByLanguageAndCountry(langCode, countryCode);
		Translation trans = transRepo.getTranslationByTextIdAndLocaleId(textId, locale.getId());
		return trans;
	}

	/**
	 * Provide a splitted locale code (e.g. de-CH --> de, CH) and a text id to
	 * receive a translation id.
	 * 
	 * @param textId
	 * @param languageCode
	 * @param countryCode
	 * @return
	 */
	public Translation getTranslationByTextIdAndLocaleCode(Long textId, String languageCode, String countryCode) {
		Translation trans = transRepo.getTranslationByTextIdAndLocaleCode(textId, languageCode, countryCode);
		return trans;
	}

	/**
	 * Fetches all translations even if there is none for a locale.
	 * 
	 * @return
	 */
	public List<Translation> getAllTranslationsByTextId(Long textId) {

		if (textId == null)
			return null;

		List<Translation> translations = new ArrayList<Translation>();
		Text textUnit = textRepo.getText(textId);

		List<Locale> locales = localeRepo.getAllLocales();
		for (Locale locale : locales) {
			Translation trans;
			trans = transRepo.getTranslationByTextIdAndLocaleId(textId, locale.getId());
			if (trans == null) {
				// check if there is a translation for language. e.g. de-CH -->
				// de
				trans = new Translation();
				trans.setLocale(locale);
				trans.setTextUnit(textUnit);

				Translation superTrans = transRepo.getTranslationByTextIdAndLanguageCode(textId, locale.getLanguage()
						.getLanguageCode());
				if (superTrans != null) {
					trans.setTranslation(superTrans.getTranslation());
					trans.setTranslationFromParent(true);
				} else {
					// DO NOT set the default text. Grab it when fetching.
					// trans.setTranslation(textUnit.getDefaultText());
				}
			}
			translations.add(trans);
		}
		return translations;
	}


	public void transChanged() {
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
				.getRequest();
		Map<String, String[]> map = req.getParameterMap();
		LOGGER.info(map.size());
	}

}
